package cn.wzc.crystal.platform.util;


public class CryStringUtil {


	/**
	 * 下划线转驼峰
	 * 例如：user_name -> userName
	 */
	public static String toCamelCase(String name) {
		if (name == null) {
			return null;
		}
		name = name.toLowerCase();
		StringBuilder sb = new StringBuilder(name.length());
		boolean upperCase = false;
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (c == '_') {
				upperCase = true;
			} else if (upperCase) {
				sb.append(Character.toUpperCase(c));
				upperCase = false;
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 驼峰转下划线
	 *
	 * @param name 驼峰命名
	 * @return 下划线命名
	 */
	public String toUnderline(String name) {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < name.length(); ++i) {
			char ch = name.charAt(i);
			if (ch >= 'A' && ch <= 'Z') {
				char ch_case = (char) (ch + 32);
				if (i > 0) {
					buf.append('_');
				}
				buf.append(ch_case);
			} else {
				buf.append(ch);
			}
		}
		return buf.toString();
	}


}
